/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.client.ZDataSource;
/*     */ import com.zimbra.client.ZFolder;
/*     */ import com.zimbra.client.ZFolder.View;
/*     */ import com.zimbra.client.ZImapDataSource;
/*     */ import com.zimbra.client.ZMailbox;
/*     */ import com.zimbra.client.ZMessage;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.soap.type.DataSource.ConnectionType;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import junit.framework.TestCase;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class TestImapImport
/*     */   extends TestCase
/*     */ {
/*     */   private static final String REMOTE_USER_NAME = "testimapimportremote";
/*     */   private static final String LOCAL_USER_NAME = "testimapimportlocal";
/*     */   private static final String NAME_PREFIX = "TestImapImport";
/*     */   private static final String DS_FOLDER_ROOT = "/TestImapImport";
/*     */   private static final String REMOTE_PATH_F1 = "/TestImapImport-f1";
/*     */   private static final String REMOTE_PATH_F2 = "/TestImapImport-f1/TestImapImport-f2";
/*     */   private static final String REMOTE_PATH_F3 = "/TestImapImport-f3";
/*     */   private static final String REMOTE_PATH_F4 = "/TestImapImport-f3/TestImapImport-f4";
/*     */   private static final String LOCAL_PATH_F1 = "/TestImapImport/TestImapImport-f1";
/*     */   private static final String LOCAL_PATH_F2 = "/TestImapImport/TestImapImport-f1/TestImapImport-f2";
/*     */   private static final String LOCAL_PATH_F3 = "/TestImapImport/TestImapImport-f3";
/*     */   private static final String LOCAL_PATH_F4 = "/TestImapImport/TestImapImport-f3/TestImapImport-f4";
/*     */   private static final String LOCAL_PATH_INBOX = "/TestImapImport/INBOX";
/*     */   private static final String LOCAL_PATH_TRASH = "/TestImapImport/Trash";
/*     */   private ZMailbox mRemoteMbox;
/*     */   private ZMailbox mLocalMbox;
/*     */   private String mOriginalCleartextValue;
/*     */   private ZDataSource mDataSource;
/*     */   private boolean mOriginalEnableStarttls;
/*     */   
/*     */   public void setUp()
/*     */     throws Exception
/*     */   {
/*  68 */     cleanUp();
/*     */     
/*     */ 
/*  71 */     if (!TestUtil.accountExists("testimapimportlocal")) {
/*  72 */       TestUtil.createAccount("testimapimportlocal");
/*     */     }
/*  74 */     if (!TestUtil.accountExists("testimapimportremote")) {
/*  75 */       TestUtil.createAccount("testimapimportremote");
/*     */     }
/*  77 */     this.mRemoteMbox = TestUtil.getZMailbox("testimapimportremote");
/*  78 */     this.mLocalMbox = TestUtil.getZMailbox("testimapimportlocal");
/*     */     
/*     */ 
/*  81 */     ZFolder folder = this.mLocalMbox.getFolderByPath("/TestImapImport");
/*  82 */     if (folder == null) {
/*  83 */       folder = TestUtil.createFolder(this.mLocalMbox, "TestImapImport");
/*     */     }
/*     */     
/*     */ 
/*  87 */     int port = Integer.parseInt(TestUtil.getServerAttr("zimbraImapBindPort"));
/*  88 */     this.mDataSource = new ZImapDataSource("TestImapImport", true, "localhost", port, "testimapimportremote", "test123", folder.getId(), DataSource.ConnectionType.cleartext);
/*     */     
/*  90 */     String id = this.mLocalMbox.createDataSource(this.mDataSource);
/*  91 */     this.mDataSource = null;
/*  92 */     for (ZDataSource ds : this.mLocalMbox.getAllDataSources()) {
/*  93 */       if (ds.getId().equals(id)) {
/*  94 */         this.mDataSource = ds;
/*     */       }
/*     */     }
/*  97 */     assertNotNull(this.mDataSource);
/*     */     
/*     */ 
/* 100 */     this.mOriginalCleartextValue = TestUtil.getServerAttr("zimbraImapCleartextLoginEnabled");
/* 101 */     TestUtil.setServerAttr("zimbraImapCleartextLoginEnabled", "TRUE");
/*     */     
/*     */ 
/* 104 */     this.mOriginalEnableStarttls = LC.javamail_imap_enable_starttls.booleanValue();
/* 105 */     LC.javamail_imap_enable_starttls.setDefault(Boolean.toString(false));
/*     */   }
/*     */   
/*     */ 
/*     */   public void testImapImport()
/*     */     throws Exception
/*     */   {
/* 112 */     ZimbraLog.test.info("Testing adding message to remote inbox.");
/* 113 */     String remoteQuery = "in:inbox msg1";
/* 114 */     TestUtil.addMessage(this.mRemoteMbox, "TestImapImport msg1", Integer.toString(2), "u");
/* 115 */     checkMsgCount(this.mRemoteMbox, remoteQuery, 1);
/* 116 */     assertNull(this.mLocalMbox.getFolderByPath("/TestImapImport/INBOX"));
/* 117 */     List<ZMessage> msgs = TestUtil.search(this.mRemoteMbox, remoteQuery);
/* 118 */     assertEquals("Message count in remote inbox", 1, msgs.size());
/* 119 */     ZMessage msg = (ZMessage)msgs.get(0);
/* 120 */     assertTrue("Remote message is read", msg.isUnread());
/*     */     
/* 122 */     importImap();
/*     */     
/* 124 */     String localInboxQuery = "in:/TestImapImport/INBOX";
/* 125 */     checkMsgCount(this.mLocalMbox, localInboxQuery, 1);
/* 126 */     msgs = TestUtil.search(this.mRemoteMbox, remoteQuery);
/* 127 */     msg = (ZMessage)msgs.get(0);
/* 128 */     assertTrue("Remote message is read", msg.isUnread());
/* 129 */     compare();
/*     */     
/*     */ 
/*     */ 
/* 133 */     ZimbraLog.test.info("Testing flag.");
/* 134 */     msgs = TestUtil.search(this.mRemoteMbox, remoteQuery);
/* 135 */     assertEquals("Message count in remote inbox", 1, msgs.size());
/* 136 */     msg = (ZMessage)msgs.get(0);
/* 137 */     assertTrue("Remote message is read", msg.isUnread());
/* 138 */     String remoteId = msg.getId();
/* 139 */     this.mRemoteMbox.flagMessage(remoteId, true);
/*     */     
/*     */ 
/* 142 */     msgs = TestUtil.search(this.mLocalMbox, localInboxQuery);
/* 143 */     assertEquals("Message count in local inbox", 1, msgs.size());
/* 144 */     msg = (ZMessage)msgs.get(0);
/* 145 */     assertFalse("Local message is flagged", msg.isFlagged());
/* 146 */     assertTrue("Local message is read", msg.isUnread());
/*     */     
/* 148 */     importImap();
/*     */     
/*     */ 
/* 151 */     msgs = TestUtil.search(this.mLocalMbox, localInboxQuery);
/* 152 */     assertEquals("Message count in local inbox", 1, msgs.size());
/* 153 */     msg = (ZMessage)msgs.get(0);
/* 154 */     assertTrue("Local message is flagged", msg.isFlagged());
/* 155 */     assertTrue("Local message is read", msg.isUnread());
/*     */     
/* 157 */     compare();
/*     */     
/*     */ 
/*     */ 
/* 161 */     ZimbraLog.test.info("Testing remote move to trash.");
/* 162 */     this.mRemoteMbox.trashMessage(remoteId);
/* 163 */     checkMsgCount(this.mRemoteMbox, "in:trash", 1);
/* 164 */     checkMsgCount(this.mLocalMbox, "in:trash", 0);
/* 165 */     importImap();
/* 166 */     checkMsgCount(this.mLocalMbox, "in:/TestImapImport/Trash", 1);
/* 167 */     compare();
/*     */     
/*     */ 
/*     */ 
/* 171 */     ZimbraLog.test.info("Testing folder creation.");
/* 172 */     TestUtil.createFolder(this.mRemoteMbox, "/TestImapImport-f1");
/* 173 */     TestUtil.createFolder(this.mRemoteMbox, "/TestImapImport-f1/TestImapImport-f2");
/* 174 */     TestUtil.createFolder(this.mLocalMbox, "/TestImapImport/TestImapImport-f3");
/* 175 */     TestUtil.createFolder(this.mLocalMbox, "/TestImapImport/TestImapImport-f3/TestImapImport-f4");
/* 176 */     importImap();
/*     */     
/*     */ 
/* 179 */     assertNotNull("Local folder /TestImapImport/TestImapImport-f1", this.mLocalMbox.getFolderByPath("/TestImapImport/TestImapImport-f1"));
/* 180 */     assertNotNull("Local folder /TestImapImport/TestImapImport-f1/TestImapImport-f2", this.mLocalMbox.getFolderByPath("/TestImapImport/TestImapImport-f1/TestImapImport-f2"));
/* 181 */     assertNotNull("Remote folder /TestImapImport-f3", this.mRemoteMbox.getFolderByPath("/TestImapImport-f3"));
/* 182 */     assertNotNull("Remote folder /TestImapImport-f3/TestImapImport-f4", this.mRemoteMbox.getFolderByPath("/TestImapImport-f3/TestImapImport-f4"));
/* 183 */     compare();
/*     */     
/*     */ 
/*     */ 
/* 187 */     ZimbraLog.test.info("Testing UIDVALIDITY change.");
/* 188 */     ZFolder localFolder1 = this.mLocalMbox.getFolderByPath("/TestImapImport/TestImapImport-f1");
/* 189 */     ZFolder remoteFolder1 = this.mRemoteMbox.getFolderByPath("/TestImapImport-f1");
/*     */     
/*     */ 
/* 192 */     String subject = "TestImapImport msg2";
/* 193 */     String originalId = TestUtil.addMessage(this.mLocalMbox, subject, localFolder1.getId());
/* 194 */     msgs = TestUtil.search(this.mLocalMbox, subject);
/* 195 */     assertEquals(1, msgs.size());
/* 196 */     assertEquals(originalId, ((ZMessage)msgs.get(0)).getId());
/*     */     
/*     */ 
/* 199 */     this.mRemoteMbox.renameFolder(remoteFolder1.getId(), "TestImapImport-renamed");
/* 200 */     this.mRemoteMbox.renameFolder(remoteFolder1.getId(), "TestImapImport-f1");
/* 201 */     importImap();
/*     */     
/*     */ 
/*     */ 
/* 205 */     msgs = TestUtil.search(this.mLocalMbox, subject);
/* 206 */     assertEquals(1, msgs.size());
/* 207 */     assertFalse("Message id did not change: " + originalId, originalId.equals(((ZMessage)msgs.get(0)).getId()));
/*     */     
/*     */ 
/*     */ 
/* 211 */     ZimbraLog.test.info("Testing simultaneous message add and folder delete 1.");
/* 212 */     ZFolder remoteFolder2 = this.mRemoteMbox.getFolderByPath("/TestImapImport-f1/TestImapImport-f2");
/* 213 */     TestUtil.addMessage(this.mRemoteMbox, "TestImapImport msg3", remoteFolder2.getId());
/* 214 */     ZFolder localFolder3 = this.mLocalMbox.getFolderByPath("/TestImapImport/TestImapImport-f3");
/* 215 */     this.mLocalMbox.deleteFolder(localFolder3.getId());
/* 216 */     checkMsgCount(this.mLocalMbox, "in:/TestImapImport/TestImapImport-f1/TestImapImport-f2", 0);
/* 217 */     importImap();
/*     */     
/*     */ 
/* 220 */     assertNull("Remote folder 3", this.mRemoteMbox.getFolderByPath("/TestImapImport-f3"));
/* 221 */     assertNull("Remote folder 4", this.mRemoteMbox.getFolderByPath("/TestImapImport-f3/TestImapImport-f4"));
/* 222 */     checkMsgCount(this.mLocalMbox, "in:/TestImapImport/TestImapImport-f1/TestImapImport-f2", 1);
/* 223 */     compare();
/*     */     
/*     */ 
/*     */ 
/* 227 */     ZimbraLog.test.info("Testing simultaneous message add and folder delete 2.");
/*     */     
/*     */ 
/* 230 */     checkMsgCount(this.mLocalMbox, "in:/TestImapImport/TestImapImport-f1", 1);
/* 231 */     checkMsgCount(this.mLocalMbox, "in:/TestImapImport/TestImapImport-f1/TestImapImport-f2", 1);
/* 232 */     checkMsgCount(this.mRemoteMbox, "in:/TestImapImport-f1", 1);
/* 233 */     checkMsgCount(this.mRemoteMbox, "in:/TestImapImport-f1/TestImapImport-f2", 1);
/*     */     
/* 235 */     ZFolder localFolder2 = this.mLocalMbox.getFolderByPath("/TestImapImport/TestImapImport-f1/TestImapImport-f2");
/* 236 */     TestUtil.addMessage(this.mLocalMbox, "TestImapImport msg4", localFolder2.getId());
/* 237 */     checkMsgCount(this.mLocalMbox, "in:/TestImapImport/TestImapImport-f1/TestImapImport-f2", 2);
/*     */     
/* 239 */     remoteFolder1 = this.mRemoteMbox.getFolderByPath("/TestImapImport-f1");
/* 240 */     this.mRemoteMbox.deleteFolder(remoteFolder1.getId());
/* 241 */     importImap();
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 246 */     assertNotNull("Local folder 1", this.mLocalMbox.getFolderByPath("/TestImapImport/TestImapImport-f1"));
/* 247 */     assertNotNull("Local folder 2", this.mLocalMbox.getFolderByPath("/TestImapImport/TestImapImport-f1/TestImapImport-f2"));
/* 248 */     checkMsgCount(this.mLocalMbox, "in:/TestImapImport/TestImapImport-f1", 0);
/* 249 */     checkMsgCount(this.mLocalMbox, "in:/TestImapImport/TestImapImport-f1/TestImapImport-f2", 1);
/* 250 */     checkMsgCount(this.mLocalMbox, "in:/TestImapImport/TestImapImport-f1/TestImapImport-f2 subject:msg4", 1);
/* 251 */     compare();
/*     */     
/*     */ 
/*     */ 
/* 255 */     ZimbraLog.test.info("Testing sync from local to remote.");
/* 256 */     checkMsgCount(this.mLocalMbox, "in:/TestImapImport/INBOX", 0);
/* 257 */     ZFolder localInbox = this.mLocalMbox.getFolderByPath("/TestImapImport/INBOX");
/* 258 */     TestUtil.addMessage(this.mLocalMbox, "TestImapImport msg5", localInbox.getId());
/* 259 */     checkMsgCount(this.mLocalMbox, "in:/TestImapImport/INBOX", 1);
/* 260 */     checkMsgCount(this.mRemoteMbox, "in:inbox", 0);
/*     */     
/*     */ 
/* 263 */     checkMsgCount(this.mLocalMbox, "in:/TestImapImport/Trash", 1);
/* 264 */     ZFolder localTrash = this.mLocalMbox.getFolderByPath("/TestImapImport/Trash");
/* 265 */     this.mLocalMbox.emptyFolder(localTrash.getId());
/* 266 */     checkMsgCount(this.mLocalMbox, "in:/TestImapImport/Trash", 0);
/* 267 */     checkMsgCount(this.mRemoteMbox, "in:trash", 1);
/* 268 */     importImap();
/*     */     
/*     */ 
/* 271 */     checkMsgCount(this.mRemoteMbox, "in:inbox msg5", 1);
/* 272 */     checkMsgCount(this.mRemoteMbox, "in:trash", 0);
/* 273 */     compare();
/*     */   }
/*     */   
/*     */   private void checkMsgCount(ZMailbox mbox, String query, int expectedCount) throws Exception {
/* 277 */     List<ZMessage> msgs = TestUtil.search(mbox, query);
/* 278 */     assertEquals("Result size for query '" + query + "'", expectedCount, msgs.size());
/*     */   }
/*     */   
/*     */   private void importImap() throws Exception {
/* 282 */     TestUtil.importDataSource(this.mDataSource, this.mLocalMbox, this.mRemoteMbox);
/*     */   }
/*     */   
/*     */   private void compare() throws Exception
/*     */   {
/* 287 */     ZFolder folder1 = this.mRemoteMbox.getUserRoot();
/* 288 */     ZFolder folder2 = this.mLocalMbox.getFolderByPath("/TestImapImport");
/* 289 */     compare(this.mRemoteMbox, folder1, this.mLocalMbox, folder2);
/*     */   }
/*     */   
/*     */   private void compare(ZMailbox mbox1, ZFolder folder1, ZMailbox mbox2, ZFolder folder2) throws Exception {
/* 293 */     assertNotNull(mbox1);
/* 294 */     assertNotNull(folder1);
/* 295 */     assertNotNull(mbox2);
/* 296 */     assertNotNull(folder2);
/*     */     
/*     */ 
/* 299 */     for (ZFolder child1 : folder1.getSubFolders()) {
/* 300 */       if (isMailFolder(child1)) {
/* 301 */         ZFolder child2 = folder2.getSubFolderByPath(child1.getName());
/* 302 */         String msg = String.format("Could not find folder %s/%s for %s", new Object[] { folder2.getPath(), child1.getName(), mbox2.getName() });
/*     */         
/* 304 */         assertNotNull(msg, child2);
/* 305 */         compare(mbox1, child1, mbox2, child2);
/*     */       }
/*     */     }
/* 308 */     assertEquals("Message count doesn't match (folder1 = " + folder1 + ", folder2 = " + folder2 + ")", folder1.getMessageCount(), folder2.getMessageCount());
/*     */     
/*     */ 
/*     */ 
/* 312 */     if ((!folder1.getPath().equals("/")) && (!folder2.getPath().equals("/"))) {
/* 313 */       List<ZMessage> msgs1 = TestUtil.search(mbox1, "in:" + folder1.getPath());
/* 314 */       List<ZMessage> msgs2 = TestUtil.search(mbox2, "in:" + folder2.getPath());
/* 315 */       compareMessages(msgs1, msgs2);
/*     */     }
/*     */   }
/*     */   
/*     */   private boolean isMailFolder(ZFolder folder) {
/* 320 */     ZFolder.View view = folder.getDefaultView();
/* 321 */     return (view == null) || (view == ZFolder.View.message) || (view == ZFolder.View.conversation);
/*     */   }
/*     */   
/*     */   private void compareMessages(List<ZMessage> msgs1, List<ZMessage> msgs2) throws Exception
/*     */   {
/* 326 */     Map<String, ZMessage> msgMap = new HashMap();
/* 327 */     for (ZMessage msg : msgs1) {
/* 328 */       msgMap.put(msg.getMessageIdHeader(), msg);
/*     */     }
/*     */     
/*     */ 
/* 332 */     for (ZMessage msg2 : msgs2) {
/* 333 */       String id = msg2.getMessageIdHeader();
/* 334 */       ZMessage msg1 = (ZMessage)msgMap.remove(id);
/* 335 */       assertNotNull("Found message '" + msg2.getSubject() + "' in mbox2 but not in mbox1", msg1);
/* 336 */       assertEquals("Message content", msg1.getContent(), msg2.getContent());
/* 337 */       String f1 = msg1.getFlags() != null ? msg1.getFlags() : "";
/* 338 */       String f2 = msg2.getFlags() != null ? msg2.getFlags() : "";
/* 339 */       assertEquals("Flags for message '" + msg1.getSubject() + "' don't match", f1, f2);
/*     */     }
/*     */     
/*     */ 
/* 343 */     if (msgMap.size() != 0) {
/* 344 */       List<String> subjects = new ArrayList();
/* 345 */       for (ZMessage msg : msgMap.values()) {
/* 346 */         subjects.add(msg.getSubject());
/*     */       }
/* 348 */       fail("Found messages in mbox1 but not in mbox2: " + StringUtil.join(",", subjects));
/*     */     }
/*     */   }
/*     */   
/*     */   public void tearDown() throws Exception
/*     */   {
/* 354 */     cleanUp();
/* 355 */     TestUtil.setServerAttr("zimbraImapCleartextLoginEnabled", this.mOriginalCleartextValue);
/* 356 */     LC.javamail_imap_enable_starttls.setDefault(Boolean.toString(this.mOriginalEnableStarttls));
/*     */   }
/*     */   
/*     */   public void cleanUp() throws Exception {
/* 360 */     if (TestUtil.accountExists("testimapimportremote")) {
/* 361 */       TestUtil.deleteAccount("testimapimportremote");
/*     */     }
/* 363 */     if (TestUtil.accountExists("testimapimportlocal")) {
/* 364 */       TestUtil.deleteAccount("testimapimportlocal");
/*     */     }
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws Exception {
/* 369 */     TestUtil.cliSetup();
/* 370 */     TestUtil.runTest(TestImapImport.class);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestImapImport.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */